<template>
  <div class="whole_black">
    <el-card class="box-card">
      <h3 slot="header" class="login-total">注册中心</h3>
      <div>
        <el-form
          v-loading="loading"
          :rules="userFormRules"
          ref="ruleForm"
          label-position="right"
          label-width="70px"
          :model="userForm"
        >
          <el-row>
            <el-col :span="12">
              <div>
                <el-form-item label="姓名" prop="username">
                  <el-input v-model="userForm.username" placeholder="请输入姓名" clearable></el-input>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="12">
              <div>
                <el-form-item label="学号" prop="cardNum">
                  <el-input v-model="userForm.cardNum" placeholder="请输入学号" clearable></el-input>
                </el-form-item>
              </div>
            </el-col>

            <el-col :span="12">
              <div>
                <el-form-item label="密码" prop="firstPassword" show-password>
                  <el-input
                    type="password"
                    v-model="userForm.firstPassword"
                    placeholder="请输入密码"
                    clearable
                    maxlength="16"
                    show-word-limit
                  ></el-input>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="12">
              <div>
                <el-form-item label="确认密码" prop="secondPassword" show-password>
                  <el-input
                    type="password"
                    v-model="userForm.secondPassword"
                    placeholder="再次输入密码"
                    clearable
                    maxlength="16"
                    show-word-limit
                  ></el-input>
                </el-form-item>
              </div>
            </el-col>

            <el-col :span="12">
              <div>
                <el-form-item label="班级" prop="classId">
                  <el-select v-model="userForm.classId" clearable placeholder="请选择">
                    <el-option
                      v-for="item in classOptions"
                      :key="item.classId"
                      :label="item.className"
                      :value="item.classId"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </div>
            </el-col>

            <el-col :span="12">
              <div>
                <el-form-item label="性别" prop="userSex" show-password>
                  <el-radio v-model="userForm.userSex" label="男">男</el-radio>
                  <el-radio v-model="userForm.userSex" label="女">女</el-radio>
                </el-form-item>
              </div>
            </el-col>

            <el-col :span="12">
              <div>
                <el-form-item label="手机号" prop="userTel">
                  <el-input v-model="userForm.userTel" placeholder="请输入手机号" clearable></el-input>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="12">
              <div>
                <el-form-item label="邮箱" prop="userEmail" show-password>
                  <el-input
                    class="emailClass"
                    v-model="userForm.userEmail"
                    placeholder="请输入邮箱"
                    clearable
                  ></el-input>

                  <el-tooltip class="item" effect="dark" content="点击发送验证码" placement="right">
                    <el-button
                      @click="sendEmail"
                      type="success"
                      icon="el-icon-message"
                      size="mini"
                      circle
                    ></el-button>
                  </el-tooltip>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="12">
              <div>
                <el-form-item label="验证码" prop="code" show-password>
                  <el-input class="emailClass" v-model="userForm.code" placeholder="验证码" clearable></el-input>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="24">
              <div>
                <el-form-item>
                  <el-button type="primary" class="userRegister" @click="submitForm('ruleForm')">注册</el-button>
                  <el-button type="info" @click="backLogin()">返回</el-button>
                </el-form-item>
              </div>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "Welcome",
  data() {
    var checkPwd = (rule, value, callback) => {
      let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
      let re = new RegExp(reg);
      if (!value) {
        return callback(new Error("密码不能为空"));
      } else if (!re.test(value)) {
        return callback(new Error("密码只能是数字与字母组合，且6-16位"));
      } else {
        callback();
      }
    };

    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.userForm.firstPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      userForm: {
        username: "",
        cardNum: "",
        userEmail: "",
        userSex: "男",
        firstPassword: "",
        secondPassword: "",
        code: "",
        // 班级号
        classId: ""
      },
      userFormRules: {
        username: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        cardNum: [
          { required: true, message: "学号不能为空", trigger: "blur" },
          { min: 9, max: 9, message: "学号是9位", trigger: "blur" }
        ],
        firstPassword: [{ validator: checkPwd, trigger: "change" }],
        secondPassword: [{ validator: validatePass2, trigger: "change" }],
        userEmail: [
          { required: true, message: "邮箱不能为空", trigger: "blur" },
          { type: "email", message: "邮箱格式不对吧！", trigger: "blur" }
        ],
        code: [{ required: true, message: "验证码不能为空", trigger: "blur" }],
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        classId: [
          { required: true, message: "学号不能为空", trigger: "请选择班级" }
        ],
        userSex: [{ required: true, message: "性别不能为空", trigger: "blur" }],
        id: [{ required: true, message: "班级不能为空", trigger: "blur" }],
        userTel: [
          // 添加正则表达式 pattern: /^1[3|5|7|8|9]\d{9}$/，验证手机号是否正确
          { required: true, message: "请输入手机号", trigger: "change" },
          {
            pattern: /^1[3456789]\d{9}$/,
            message: "请输入正确的号码格式",
            trigger: "change"
          }
        ]
      },
      loading: false,
      classOptions: [
        {
          id: 1,
          className: ""
        }
      ]
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 开启动画
          this.loading = true;
          this.userRegister();
          // 3秒没有相应则关闭动画
        } else {
          this.$message.warning({
            message: "请先完整信息！",
            center: true,
            showClose: true
          });
        }
      });
    },
    userRegister() {
      this.$http
        .post(`user/noAuthorize/entity/${this.userForm.code}`, this.userForm)
        .then(resp => {
          setTimeout(() => {
            this.loading = false;
          }, 3000);
          let { code, msg } = resp.data;
          if (code === 1) {
            this.$router.push("/login");
            return this.$notify({
              title: "叮咚！叮咚！",
              message: "注册成功了",
              type: "success",
              position: "top-left"
            });
          }
          return this.$notify({
            title: "您有一条消息！",
            message: msg,
            type: "error",
            position: "top-left"
          });
        })
        .catch(resp => {
          setTimeout(() => {
            this.loading = false;
          }, 3000);
        });
    },
    sendEmail() {
      this.$refs.ruleForm.validateField("userEmail", errorMsg => {
        if (errorMsg) {
          return false;
        } else {
          this.$http
            .get(`user/noAuthorize/email/${this.userForm.userEmail}`)
            .then(resp => {
              setTimeout(() => {
                this.loading = false;
              }, 3000);
              let { code, msg } = resp.data;
              if (code === 1) {
                return this.$notify({
                  title: "叮咚！叮咚！",
                  message: "发送成功，验证码5分钟有效",
                  type: "success",
                  position: "top-left"
                });
              }
              return this.$notify({
                title: "您有一条消息！",
                message: msg,
                type: "error",
                position: "top-left"
              });
            })
            .catch(resp => {});
        }
      });
    },
    backLogin() {
      this.$router.push("/login");
    },
    getAllClassList() {
      this.$http
        .get("class/noAuthorize/all/class")
        .then(resp => {
          let { code, msg, data } = resp.data;
          if (code == 1) {
            this.classOptions = data.classesList;
          }
        })
        .catch(resp => {});
    }
  },
  created() {
    this.getAllClassList();
  }
};
</script >
 
<style scoped>
.whole_black {
  top: 0;
  bottom: 0;
  position: fixed;
  overflow-y: scroll;
  overflow-x: hidden;
  width: 100%;
  height: 100%;
  background-image: url(../../assets/bg.jpg);
  background-repeat: no-repeat;
  background-size: 100% 100%;
}
.box-card {
  border-radius: 15px;
  width: 760px;
  height: 500px;
  margin-left: 285px;
  margin-top: 20px;
  box-shadow: 10px 10px 10px;
}

.login-total {
  text-align: center;
}

.img-wechat {
  width: 30px;
  height: 30px;
  margin-left: 135px;
}

.imgLink {
  width: 30px;
  height: 30px;
}
.emailClass {
  width: 200px;
}
.userRegister {
  margin-top: 5px;
  margin-left: 200px;
}

.el-input {
  width: 220px;
}
.el-form-item {
  margin-left: 5px;
}
</style>